import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:im_app/im/page/view/ListSeparator.dart';
import 'package:im_app/im/page/view/LoadingView.dart';
import 'package:im_app/im/util/UiUtil.dart';

import '../../pojo/ImPojo.dart';
import '../../util/ImConstant.dart';
import '../bloc/CollectSelectCubit.dart';
import '../view/component/pane/MessagePane.dart';

// 选择收藏
class CollectSelectPage extends StatelessWidget {
  const CollectSelectPage({super.key});

  @override
  Widget build(BuildContext context) {
    var collectSelectCubit = context.watch<CollectSelectCubit>();
    return Scaffold(
      appBar: _buildAppBar(context),
      backgroundColor: ImConstant.IM_MAIN_BACKGROUND_COLOR,
      body: _buildBody(context, collectSelectCubit),
    );
  }

  AppBar _buildAppBar(BuildContext context) {
    return AppBar(
      backgroundColor: ImConstant.IM_MAIN_BACKGROUND_COLOR,
      leading: GestureDetector(
        child: const Padding(
          padding: EdgeInsets.only(left: 15, top: 15),
          child: Text(
            '取消',
            style: TextStyle(color: Colors.blue),
          ),
        ),
        onTap: () => Navigator.of(context).pop(),
      ),
      centerTitle: true,
      title: const Text(
        '选择收藏',
        style: TextStyle(color: Colors.black, fontSize: ImConstant.IM_DEFAULT_SIZE),
      ),
    );
  }

  Widget _buildBody(BuildContext context, CollectSelectCubit collectSelectCubit) {
    return LoadingView(
        state: collectSelectCubit.state.loadingState,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Expanded(child: _buildCollectListPane(context, collectSelectCubit)),
            _buildSubmitPane(context, collectSelectCubit)
          ],
        ));
  }

  Widget _buildCollectListPane(BuildContext context, CollectSelectCubit collectSelectCubit) {
    return DecoratedBox(
      decoration: const BoxDecoration(color: ImConstant.IM_RESERVE_BACKGROUND_COLOR),
      child: ListView.separated(
        itemBuilder: (BuildContext context, int index) {
          return CollectSelectPane(imCollect: collectSelectCubit.state.collects[index]);
        },
        itemCount: collectSelectCubit.state.collects.length,
        separatorBuilder: (BuildContext context, int index) {
          return ListSeparator(
            indent: 0,
            endIndent: 0,
          );
        },
      ),
    );
  }

  Widget _buildSubmitPane(BuildContext context, CollectSelectCubit collectSelectCubit) {
    var canEvent = collectSelectCubit.state.isDoing || collectSelectCubit.state.selectCollects.isNotEmpty;
    return Container(
      decoration: BoxDecoration(color: ImConstant.IM_MAIN_BACKGROUND_COLOR),
      padding: const EdgeInsets.only(top: 10, bottom: 10),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          TextButton( // 转发
              onPressed: !canEvent ? null : ()=>_submitEvent(context, collectSelectCubit),
              child: const Text('提交')
          )
        ],
      ),
    );
  }

  void _submitEvent(BuildContext context, CollectSelectCubit collectSelectCubit) {
    if (collectSelectCubit.state.selectCollects.isEmpty) {
      UiUtil.showSnackBar(context, "请选择");
      return;
    }
    collectSelectCubit.add(CollectSelectSubmitEvent(context));
  }
}

class CollectSelectPane extends StatelessWidget {
  final ImCollect imCollect;

  const CollectSelectPane({required this.imCollect, super.key});

  @override
  Widget build(BuildContext context) {
    var collectSelectCubit = context.watch<CollectSelectCubit>();

    Size scrSize = MediaQuery.of(context).size;
    double screenWidth = scrSize.width - 60;

    var isSelected = collectSelectCubit.state.selectCollects.contains(imCollect);

    return Container(
      padding: const EdgeInsets.only(left: 20, right: 20),
      color: isSelected ? ImConstant.IM_MAIN_BACKGROUND_COLOR : Colors.transparent,
      child: Row(
        children: [
          Radio(
            value: imCollect.id!,
            groupValue: collectSelectCubit.state.selectCollects.contains(imCollect) ? imCollect.id! : 0,
            onChanged: (int? value) {
              if (isSelected) {
                collectSelectCubit.add(CollectSelectRemoveCollectEvent(imCollect));
              } else {
                collectSelectCubit.add(CollectSelectAddCollectEvent(imCollect));
              }
            },
          ),
          const SizedBox(
            width: 10,
          ),
          GestureDetector(
            onTap: () {
              if (isSelected) {
                collectSelectCubit.add(CollectSelectRemoveCollectEvent(imCollect));
              } else {
                collectSelectCubit.add(CollectSelectAddCollectEvent(imCollect));
              }
            },
            child: Expanded(
                child: AbsorbPointer(
              child: MessageRefPane(
                message: imCollect.message!,
                param: MessageRefParam(maxLines: 100, refMaxLength: screenWidth),
              ),
            )),
          )
        ],
      ),
    );
  }
}
